import { ActionTypes, AssetImages, HttpAPI } from '../constants'
import { AxiosUtil } from '../utils'
import base from './base'
const register = 'user/register'
const login = 'user/login'
const queryUser = 'user/select'
const updateUser = 'user/update'
const relateUser = 'user/addrelate'
const removeUser = 'user/delrelate'
export default {
    fetchRegister: function * (payload) {
        const { data, callBack = null } = payload
        console.log('fetch register', data);
        
        const { username, password, email, phone } = data
        const url = `${register}?username=${username}&password=${password}&email=${email}&phone=${phone}`
        const { response, error } = yield AxiosUtil.get(url);
        yield base.after(response, error, ActionTypes.API_REGISTER_SUCCESS)
        callBack && callBack(response, error)
    },
    fetchLogin: function * (payload) {
        const { data, callBack = null } = payload
        const { response, error } = yield AxiosUtil.get(login, data);
        yield base.after(response, error, ActionTypes.API_LOGIN_SUCCESS)
        callBack && callBack(response, error)
    },
    fetchUserById: function * (payload) {
        const { data, callBack = null } = payload
        const { response, error } = yield AxiosUtil.get(queryUser, data);
        yield base.after(response, error, ActionTypes.API_GET_USER_BY_ID_SUCCESS)
        callBack && callBack(response, error)
    },
    fetchUpdateUserInfo: function * (payload) {
        const { data, callBack = null } = payload
        const { response, error } = yield AxiosUtil.get(updateUser, data);
        yield base.after(response, error, ActionTypes.API_CHANGE_USER_INFO_SUCCESS)
        callBack && callBack(response, error)
    },
    fetchUpdateRelate: function * (payload) {
        const { data, callBack = null } = payload
        const { response, error } = yield AxiosUtil.get(relateUser, data)
        yield base.after(response, error, ActionTypes.API_RELATE_USER_BY_TYPE_SUCCESS)
        callBack && callBack(response, error);
    },
    fetchRemoveRelate: function * (payload) {
        const { data, callBack = null } = payload
        const { response, error } = yield AxiosUtil.get(removeUser, data);
        yield base.after(response, error, ActionTypes.API_REMOVE_RELATE_USER_BY_TYPE_SUCCESS)
        callBack && callBack(response, error);
    }
}